import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { CmtCourseChapterApi } from '#/api/cmt/course/chapter';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { z } from '#/adapter/form';
import { getRangePickerDefaultProps } from '#/utils';
import { getCourseInfoOptions } from '#/api/cmt/course/info';

/** 视频类型选项 */
export const VIDEO_TYPE_OPTIONS = [
  { label: 'MP4', value: 'mp4' },
  { label: 'M3U8', value: 'm3u8' },
  { label: '其他', value: 'other' },
];

/** 是否免费选项 */
export const IS_FREE_OPTIONS = [
  { label: '是', value: 1 },
  { label: '否', value: 0 },
];

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'courseId',
      label: '课程ID',
      rules: 'required',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择课程',
        api: getCourseInfoOptions,
        resultField: 'list',
        labelField: 'title',
        valueField: 'id',
      },
    },
    {
      fieldName: 'title',
      label: '章节标题',
      rules: 'required',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入章节标题',
      },
    },
    {
      fieldName: 'videoUrl',
      label: '视频地址',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请输入视频地址',
      },
    },
    {
      fieldName: 'videoType',
      label: '视频类型',
      component: 'Select',
      formItemClass: 'col-span-1',
      componentProps: {
        options: VIDEO_TYPE_OPTIONS,
        placeholder: '请选择视频类型',
      },
    },
    {
      fieldName: 'duration',
      label: '时长(秒)',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入时长',
        controlsPosition: 'right',
        class: '!w-full',
        min: 0,
      },
    },
    {
      fieldName: 'sort',
      label: '排序',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入排序',
        controlsPosition: 'right',
        class: '!w-full',
      },
      defaultValue: 0,
    },
    {
      fieldName: 'isFree',
      label: '是否免费试看',
      component: 'RadioGroup',
      formItemClass: 'col-span-1',
      componentProps: {
        options: IS_FREE_OPTIONS,
      },
      rules: z.number().default(0).optional(),
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'courseId',
      label: '课程ID',
      component: 'ApiSelect',
      componentProps: {
        placeholder: '请选择课程',
        api: getCourseInfoOptions,
        resultField: 'list',
        labelField: 'title',
        valueField: 'id',
      },
    },
    {
      fieldName: 'title',
      label: '章节标题',
      component: 'Input',
      componentProps: {
        clearable: true,
        placeholder: '请输入章节标题',
      },
    },
    {
      fieldName: 'videoType',
      label: '视频类型',
      component: 'Select',
      componentProps: {
        clearable: true,
        options: VIDEO_TYPE_OPTIONS,
        placeholder: '请选择视频类型',
      },
    },
    {
      fieldName: 'isFree',
      label: '是否免费',
      component: 'Select',
      componentProps: {
        clearable: true,
        options: IS_FREE_OPTIONS,
        placeholder: '请选择是否免费',
      },
    },
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        clearable: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<CmtCourseChapterApi.CourseChapter>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: 'ID',
      minWidth: 120,
    },
    {
      field: 'courseId',
      title: '课程ID',
      minWidth: 120,
    },
    {
      field: 'title',
      title: '章节标题',
      minWidth: 200,
    },
    {
      field: 'videoUrl',
      title: '视频地址',
      minWidth: 200,
    },
    {
      field: 'videoType',
      title: '视频类型',
      minWidth: 120,
    },
    {
      field: 'duration',
      title: '时长(秒)',
      minWidth: 120,
    },
    {
      field: 'sort',
      title: '排序',
      minWidth: 100,
    },
    {
      field: 'isFree',
      title: '是否免费',
      minWidth: 120,
      slots: { default: 'isFree' },
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 160,
      formatter: 'formatDateTime',
    },
    {
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
